<?php
namespace App\HttpController;

use EasySwoole\Core\Http\AbstractInterface\Controller;
use Core\AbstractInterface\AbstractController;
use EasySwoole\Core\Http\Request;
use EasySwoole\Core\Http\Response;
use \EasySwoole\Core\Component\Logger;
use \EasySwoole\Core\Swoole\ServerManager;

use EasySwoole\Core\Component\Pool\PoolManager;

use \EasySwoole\Core\Utility\Validate;
use App\HttpController\CommonController;

/**
 * Class Register
 * @package App\HttpController
 */
class Register extends Controller
{
    function index()
    {
        $this->response()->write('Register easySwoole!');
    }
    protected function onRequest($action): ?bool
    {
    	$this->request()->withAttribute('requestTime', microtime(true));
        return parent::onRequest($action); // TODO: Change the autogenerated stub
    }
    /**
     * web注册 PC端、手机端（用手机号注册）
     */
    function register_web()
    {
        $post=$this->request()->getRequestParam();

        if( empty($post['phone']) ) {
            $this->writeJson('3000',$post, '缺少核心参数：phone ');
            return false;
        }
        $data['phone']=$post['phone'];
        if( empty($post['password']) ) {
            $this->writeJson('3000',$post, '缺少核心参数：password ');
            return false;
        }
        $common = new CommonController();
        $data['password']=$common->hash($post['password']);
        if( empty($post['system_id']) ) {
            $this->writeJson('3000',$post, '缺少核心参数：system_id ');
            return false;
        }
        $data['system_id']=$post['system_id'];

        if( !empty($post['merchant_id'])  ) {
            $data['merchant_id']=$post['merchant_id'];
        }

        if( !empty($post['shop_id'])  ) {
            $data['shop_id']=$post['shop_id'];
        }
        $pool = PoolManager::getInstance()->getPool('App\Utility\MysqlPool2'); // 获取连接池对象
        $pool_db = $pool->getObj($timeOut = 0.1);
        
        $id = $pool_db->insert ("user", $data);
        $pool->getObj($pool_db);
        $this->writeJson('200',$data, '注册成功！');
        return true;

        
    }
 	/**
	 * 微信注册 公众号、小程序（openid、unionid）
	 */
	public function register_wechat()
	{
		$this->response()->write('register_wechat ');
	}
    
	
    protected function actionNotFound($action):void
    {
        $this->response()->write('Register actionNotFound');
    }
    protected function afterAction($actionName):void
    {
    	

    	$request=$this->request();
    	//从请求里获取之前增加的时间戳
		$reqTime = $request->getAttribute('requestTime');
		//计算一下运行时间
		$runTime = round(microtime(true) - $reqTime, 3);
		//获取用户IP地址
		$ip = ServerManager::getInstance()->getServer()->connection_info($request->getSwooleRequest()->fd);

		//拼接一个简单的日志
		$logStr = ' | '.$ip['remote_ip'] .' | '. $runTime . '|' . $request->getUri() .' | '.
		    $request->getHeader('user-agent')[0] .'|Register.php=>afterAction';
		    //判断一下当执行时间大于1秒记录到 slowlog 文件中，否则记录到 access 文件
		if($runTime > 1){
		    Logger::getInstance()->log($logStr, 'slowlog-07-23');
		}else{
		    logger::getInstance()->log($logStr,'access-07-23');
		}
    }
}
